package com.darren.befriend.fragments;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;

import com.darren.befriend.BaseActivity;
import com.darren.befriend.R;
import com.darren.befriend.adapters.InterestAdapter;
import com.darren.befriend.models.HobbyModel;
import com.parse.ParseUser;

public class HobbyBaseFragment extends Fragment implements OnClickListener,
		OnItemClickListener {

	protected String[] interestTextArr;
	protected int[] mInterestResIds;
	protected ArrayList<Boolean> mSelected;
	protected String mTitle;
	protected ParseUser mCurrentUser;

	private GridView mGridview;
	private ImageView mCheckImg;
	private ImageView mSkipImg;
	private ArrayList<HobbyModel> mInterestArr;
	protected ArrayList<HobbyModel> mInterestSelectedArr;
	private InterestAdapter mInterestAdapter;
	private final int NUMBER_COLS = 3;
	private checkHobiesListener mListener;
	protected int HOBBY_TYPE = 0;
	public static final int FIRST_MODE = 111;
	public static final int SECOND_MODE = 112;
	public static final String MODE = "mode";
	protected int MODE_VALUE = 0;

	private ArrayList<HobbyModel> createHobbiesArr(String[] hobbiesTextArr,
			int[] hobbiesResIds, ArrayList<Boolean> mSelected) {
		if (hobbiesTextArr != null && hobbiesResIds != null
				&& hobbiesTextArr.length == hobbiesResIds.length) {
			ArrayList<HobbyModel> hobbiesArr = new ArrayList<HobbyModel>();
			for (int i = 0; i < hobbiesTextArr.length; i++) {
				HobbyModel interestItem = new HobbyModel();
				interestItem.interestIconRes = hobbiesResIds[i];
				interestItem.interestText = hobbiesTextArr[i];
				if (mSelected != null && mSelected.size() > i) {

					interestItem.isSelected = mSelected.get(i);
				} else {

					interestItem.isSelected = false;
				}

				hobbiesArr.add(interestItem);
			}

			return hobbiesArr;
		}

		return null;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		ViewGroup view = (ViewGroup) inflater.inflate(
				R.layout.layout_interest_frag, container, false);
		mGridview = (GridView) view.findViewById(R.id.interest_gridview);
		mCheckImg = (ImageView) view.findViewById(R.id.check_btn);
		mSkipImg = (ImageView) view.findViewById(R.id.skip_btn);
		mCheckImg.setOnClickListener(this);
		mSkipImg.setOnClickListener(this);
		return view;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {

			mListener = (checkHobiesListener) activity;
		} catch (ClassCastException e) {

			throw new ClassCastException(activity.toString()
					+ " must implement checkHobiesListener");
		}
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		if (getActivity() != null) {
			mCurrentUser = ((BaseActivity) getActivity()).getCurrentUser();
			if (interestTextArr != null && mInterestResIds != null) {

				mInterestArr = createHobbiesArr(interestTextArr,
						mInterestResIds, mSelected);
				int count = mInterestArr.size() % NUMBER_COLS;
				if (count < NUMBER_COLS) {
					int emptyItems = 0;
					if (count == 0) {

						emptyItems = 3;
					} else {

						emptyItems = (NUMBER_COLS - count) + NUMBER_COLS;
					}

					for (int i = 0; i < emptyItems; i++) {

						HobbyModel item = new HobbyModel();
						item.isEmpty = true;
						mInterestArr.add(item);
					}
				}

				mInterestAdapter = new InterestAdapter(getActivity(),
						mInterestArr);
				mGridview.setNumColumns(3);
				mGridview.setAdapter(mInterestAdapter);
				mGridview.setOnItemClickListener(this);
			}
		}

		mInterestSelectedArr = new ArrayList<HobbyModel>();
		for (int i = 0; i < interestTextArr.length; i++) {

			HobbyModel item = new HobbyModel();
			mInterestSelectedArr.add(i, item);
		}
	}

	public void saveSettingItems(int type) {

		if (mListener != null) {
			if (mInterestArr != null) {
				for (int i = 0; i < mInterestArr.size(); i++) {
					if (!mInterestArr.get(i).isEmpty) {

						mInterestSelectedArr.remove(i);
						mInterestSelectedArr.add(i, mInterestArr.get(i));
					}
				}
			}

			mListener.onClickCheckHobies(type, mInterestSelectedArr);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.check_btn:

			saveSettingItems(HOBBY_TYPE);
			break;
		case R.id.skip_btn:
			mListener.onClickSkip();
			break;

		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		mInterestArr.get(position).isSelected = !mInterestArr.get(position).isSelected;
		mInterestAdapter.notifyDataSetChanged();
	}

	public ArrayList<HobbyModel> getSelecedArr() {
		if (mInterestArr != null) {
			for (int i = 0; i < mInterestArr.size(); i++) {
				if (!mInterestArr.get(i).isEmpty) {
					mInterestSelectedArr.remove(i);
					mInterestSelectedArr.add(i, mInterestArr.get(i));
				}
			}
		}
		return mInterestSelectedArr;
	}

	public interface checkHobiesListener {
		public void onClickCheckHobies(int type,
				ArrayList<HobbyModel> selectedItems);

		public void onClickSkip();
	}
}
